package com.edu.atams.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.atams.entity.StudentClass;
import com.edu.atams.mapper.StudentClassMapper;
import com.edu.atams.service.ClassService;
import com.edu.atams.service.GradeService;
import com.edu.atams.service.StudentClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author czj666
 * @since 2021-10-09
 */
@Service
public class StudentClassServiceImpl extends ServiceImpl<StudentClassMapper, StudentClass> implements StudentClassService {

    @Autowired
    ClassService classService;

    @Autowired
    GradeService gradeService;


    @Override
    public int getCurrentClassIdByStudentId(Integer studentId) {
        List<StudentClass> studentClasses = this.list(new QueryWrapper<StudentClass>().eq("student_id", studentId));
        int currentYear = DateUtil.thisYear();
        int currentClassId = 0;
        for (StudentClass studentClass : studentClasses) {
            if (gradeService.getById(classService.getById(studentClass.getClassId()).getGradeId()).getYear() == currentYear){
                currentClassId = studentClass.getClassId();
                break;
            }
        }
        return  currentClassId;
    }
}
